package com.greenleaf.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenleaf.entity.Address;
import com.greenleaf.entity.Vip;
import com.greenleaf.service.AddressService;

@Controller
@RequestMapping("/Address")
public class AddressController {
	@Resource
	private AddressService addressService;
	
	@RequestMapping("/get")
	@ResponseBody
//http://localhost:8080/Greenleaf/Address/get
	public Object getAllVip(HttpServletRequest request) {
		Vip vip=(Vip) request.getSession().getAttribute("vipInfo");
		List<Address> Address = addressService.getAddress(vip.getvId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", addressService.getTotalAddress(vip.getvId()));
		map.put("data", Address);
		return map;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	//http://localhost:8080/Greenleaf/Address/add
	public Object insertAddress(Address address,HttpServletRequest request) {
		Vip vip=(Vip) request.getSession().getAttribute("vipInfo");
		address.setvId(vip.getvId());
		if(address.getDefaule().equals("true")) {
			address.setDefaule("是");
		}else{
			address.setDefaule("否");
		}
		int r=addressService.insertAddress(address);
		return r==1?true:false;
	}
	@RequestMapping("/del")
	@ResponseBody
	//http://localhost:8080/Greenleaf/Address/del
	public Object delAddress(int adsId) {
		return addressService.delAddress(adsId);
}
}